//
//  PurchaseSubmitViewController.m
//  XYMaterial
//
//  Created by 肖扬 on 15/12/30.
//  Copyright © 2015年 肖扬. All rights reserved.
//

#import "PurchaseSubmitViewController.h"

@interface PurchaseSubmitViewController ()<UITextViewDelegate,UIAlertViewDelegate>
@property (nonatomic,strong) UITextView *inputTextView;

@end

@implementation PurchaseSubmitViewController
@synthesize supply,inputTextView;
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController setNavigationBarHidden:NO];
    [self.leftBtn addTarget: self action: @selector(goBackAction) forControlEvents: UIControlEventTouchUpInside];
    [self.view setBackgroundColor:UIColorFromRGB(0xf3f2f2)];
    [self.rightBtn setTitle:@"提交" forState:0];
    [self.rightBtn setEnabled:NO];
//    [self.rightBtn setBackgroundColor:ButtonGrayColor];
    [self.rightBtn setTitleColor:ButtonGrayColor forState:0];
    [self.rightBtn addTarget:self action:@selector(submitClick:) forControlEvents:UIControlEventTouchUpInside];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextViewTextDidChangeNotification  object:self.inputTextView];
//    UITextFieldTextDidChangeNotification
    UIView *informationView = [self createViewFrame:CGRectMake(0, 74, SWIDTH, 80) bgColor:[UIColor whiteColor] tag:0];
    [self.view addSubview:informationView];
    
    UILabel *titleLab = [self createLabelFrame:CGRectMake(10, 10, SWIDTH - 20, 15) title:self.supply.brand_name titleColor:BLACKColor tag:0];
    titleLab.font = [UIFont systemFontOfSize:14];
    [informationView addSubview:titleLab];
    if (![Unity isBlankString:self.supply.title]) {
        titleLab.text = self.supply.title;
    }else{
        NSString *title = @"";
        if (![Unity isBlankString:self.supply.brand_name]) {
            title = [title stringByAppendingString:[NSString stringWithFormat:@"%@",self.supply.brand_name]];
        }
        if (![Unity isBlankString:self.supply.material_name]) {
            title = [title stringByAppendingString:[NSString stringWithFormat:@" %@",self.supply.material_name]];
        }
        titleLab.text = title;
    }
    if ([Unity isBlankString:self.supply.province_name]) {
        self.supply.province_name = @"";
    }
    NSString *address = @"";
    if (![Unity isBlankString:self.supply.province_name]) {
        address = [NSString stringWithFormat:@"地区:%@",self.supply.province_name];
        if (![Unity isBlankString:self.supply.city_name]) {
            address = [NSString stringWithFormat:@"地区:%@%@",self.supply.province_name,self.supply.city_name];
        }
    }
    int addressWidth = [address sizeWithFont:titleLab.font
                        constrainedToSize:CGSizeMake(150, 30)
                            lineBreakMode:NSLineBreakByClipping].width;

    UILabel *addressLab = [self createLabelFrame:CGRectMake(10, 35, (SWIDTH - 20)/2, 11) title:address titleColor:GRAYColor tag:0];
    addressLab.font = [UIFont systemFontOfSize:12];
    [informationView addSubview:addressLab];
    if ([Unity isBlankString:self.supply.number]) {
        self.supply.number = @"0";
    }
    NSString *number = [NSString stringWithFormat:@"数量:%@/%@",self.supply.number,self.supply.unit];
    UILabel *numberLab = [self createLabelFrame:CGRectMake(SWIDTH/2 + 10, 35, (SWIDTH - 20)/2, 11) title:number titleColor:GRAYColor tag:0];
    numberLab.font = [UIFont systemFontOfSize:12];
    [informationView addSubview:numberLab];
    if ([Unity isBlankString:self.supply.price]) {
        self.supply.price = @"0";
    }
    NSString *price = [NSString stringWithFormat:@"%@/%@",self.supply.price,self.supply.unit];
    UILabel *priceLab = [self createLabelFrame:CGRectMake(10, 56, SWIDTH, 11) title:price titleColor:UIColorFromRGB(0xf94301) tag:0];
    priceLab.font = [UIFont systemFontOfSize:12];
    [informationView addSubview:priceLab];
    
    UIView *RemarksView = [self createViewFrame:CGRectMake(0, 166, SWIDTH, 75) bgColor:[UIColor whiteColor] tag:131];
    [self.view addSubview:RemarksView];
    
    UILabel *remarksLab = [self createLabelFrame:CGRectMake(5, 0, 50, 22) title:@"备注" titleColor:UIColorFromRGB(0xe8e8e8) tag:132];
    remarksLab.font = [UIFont systemFontOfSize:14];
    [RemarksView addSubview:remarksLab];
    
    self.inputTextView = [[UITextView alloc]initWithFrame:CGRectMake(5, 0, SWIDTH - 10, 75)];
    self.inputTextView.delegate = self;
    [self.inputTextView setBackgroundColor:[UIColor clearColor]];
    self.inputTextView.textAlignment = NSTextAlignmentLeft;
    self.inputTextView.font = [UIFont systemFontOfSize:14.0f];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [RemarksView addSubview:self.inputTextView];
    NSString * str = nil;
    if ([self.title isEqualToString:@"我要采购"]) {
        str = [NSString stringWithFormat:@"您好，请您在备注栏填写采购信息并提交，谢谢。"];
    }else if([self.title isEqualToString:@"我要供货"]){
        str = [NSString stringWithFormat:@"您好，请您在备注栏填写供货信息并提交，谢谢。"];
    }
    UILabel *lab = [self createLabelFrame:CGRectMake(10, RemarksView.frame.size.height+RemarksView.frame.origin.y +10, SWIDTH - 20, 12) title:str titleColor:UIColorFromRGB(0xf94301) tag:0];
    lab.font = [UIFont systemFontOfSize:12];
    [self.view addSubview:lab];
    
    self.view.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(viewTap:)];
    [self.view addGestureRecognizer:tap];
    
    // Do any additional setup after loading the view.
}
-(void)textChange
{
    self.rightBtn.enabled= self.inputTextView.text.length>0;
    if (self.rightBtn.enabled == YES) {
//        [self.rightBtn setBackgroundColor:ClearColor];
        [self.rightBtn setTitleColor:WhiteColor forState:0];
    }else{
//        [self.rightBtn setBackgroundColor:ButtonGrayColor];
        [self.rightBtn setTitleColor:ButtonGrayColor forState:0];
    }
}
-(void)viewTap:(UIGestureRecognizer *)tap{
    [self.inputTextView resignFirstResponder];
}
-(void)textViewDidBeginEditing:(UITextView *)textView{
    UIView *tempView = (UIView *)[self.view viewWithTag:131];
    UILabel *lab = (UILabel *)[tempView viewWithTag:132];
    if (textView.text.length==0) {
        [lab setHidden:YES];
    }
}
-(void)textViewDidEndEditing:(UITextView *)textView{
    UIView *tempView = (UIView *)[self.view viewWithTag:131];
    UILabel *lab = (UILabel *)[tempView viewWithTag:132];
    if (textView.text.length==0) {
        [lab setHidden:NO];
    }
}
-(void)purchaesIntent{
    TIME
    NSString *url = nil;
    NSMutableDictionary *body = [NSMutableDictionary dictionary];
    if ([self.title isEqualToString:@"我要采购"]) {
        url = [NSString stringWithFormat:@"%@",INTENT];
        [body setObject:self.supply.home_id forKey:@"supply_id"];
    }else if ([self.title isEqualToString:@"我要供货"]){
        url = [NSString stringWithFormat:@"%@",SUPPLYINTENT];
        [body setObject:self.supply.home_id forKey:@"purchase_id"];
    }
    HEADDIC(nowTime, url);
    [body setObject:self.supply.material_id forKey:@"material_id"];
    [body setObject:self.inputTextView.text forKey:@"comment"];
    NSString *result = [Unity requestSomeApiData:HEAD withBody:body];
    NSLog(@"result");
    NSMutableDictionary *json = [result objectFromJSONString];
    if ([[json objectForKey:@"err_code"] integerValue]==0) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:[json objectForKey:@"err_title"] message:[json objectForKey:@"err_message"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        alert.tag = 10;
        [alert show];
    }else{
        SHOW_TITLEALERT([json objectForKey:@"err_title"], [json objectForKey:@"err_message"])
    }
}
-(void)submitClick:(UIButton *)sender{
    if ([self.inputTextView.text isEqualToString:@"备注"]||[self.inputTextView.text isEqualToString:@""]) {
        SHOW_ALERT(@"请输入备注信息");
        return;
    }
    [self purchaesIntent];
    
}
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
    if (alertView.tag == 10) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
